#include "kmainwindow.h"
#include <QKeyEvent>
#include "ksettingmanager.h"
#include "ui_kmainwindow.h"
#include "kdrawinggraphicsview.h"

KMainWindow::KMainWindow(QWidget* parent)
	:QMainWindow(parent)
	,ui(new Ui::KMainWindow)
{
	setWindowIcon(QIcon(":/icons/mainWindowDraw.png"));
	ui->setupUi(this);
	loadingMainwindow();

}

KMainWindow::~KMainWindow()
{
	delete ui;
}

QGraphicsView* KMainWindow::getConvas() const
{
	return ui->m_pGraphicsView;
}

KToolPanelWidget* KMainWindow::getToolPanelWidget() const
{
	return ui->m_pToolPanelWidget;
}

KDrawingGraphicsView* KMainWindow::getDrawingView() const
{
	return ui->m_pGraphicsView;
}

KDrawSettingPanelWidget* KMainWindow::getSettingWidget() const
{
	return ui->m_pDrawSettingPanelWidget;
}

QLabel* KMainWindow::getViewCoordinateLabel() const
{
	return ui->m_pLabelViewCoordinate;
}

QLabel* KMainWindow::getSceneCoordinateLabel() const
{
	return ui->m_pLabelSceneCoordinate;
}

QLabel* KMainWindow::getItemCoordinateLabel() const
{
	return ui->m_pLabelItemCoordinate;
}

QAction* KMainWindow::getActionNewSvg() const
{
	return ui->m_pActionNewSVG;
}

QAction* KMainWindow::getActionOpenSvg() const {
	return ui->m_pActionOpenSVG;
}

QAction* KMainWindow::getActionSaveSvg() const {
	return ui->m_pActionSaveSVG;
}

QAction* KMainWindow::getActionExportSvg() const {
	return ui->m_pActionExportSVG;
}

QAction* KMainWindow::getActionExportPng() const {
	return ui->m_pActionExportPNG;
}

QTabWidget* KMainWindow::getTabWidget() const
{
	return ui->m_pTabWidget;
}

void KMainWindow::closeEvent(QCloseEvent* event)
{
	KSettingManager::getInstance().setMainWindowSetting("Size", size());
	KSettingManager::getInstance().setMainWindowSetting("Pos", pos());
	KSettingManager::getInstance().setMainWindowSetting("IsMaximized", isMaximized());
	QMainWindow::closeEvent(event);
}

void KMainWindow::keyPressEvent(QKeyEvent* event)
{
	if (event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_S)
	{
		emit requestSave(); 
		event->accept();
		return;
	}

	if (event->modifiers() == (Qt::ControlModifier | Qt::ShiftModifier) && event->key() == Qt::Key_S) {
		emit requestSaveAs();
		event->accept();
		return;
	}

	if (event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_O) {
		emit requestOpen();
		event->accept();
		return;
	}

	if (event->modifiers() == Qt::ControlModifier && event->key() == Qt::Key_E) {
		emit requestExportPng();
		event->accept();
		return;
	}
	QMainWindow::keyPressEvent(event); 
}

void KMainWindow::loadingMainwindow()
{
	QSize size = KSettingManager::getInstance().getMainWindowSetting("Size", QSize(1400, 900)).toSize();
	QPoint pos = KSettingManager::getInstance().getMainWindowSetting("Pos", QPoint(1200, 500)).toPoint();
	if (KSettingManager::getInstance().getMainWindowSetting("IsMaximized", false).toBool()) 
	{
		showMaximized();
	}
	else 
	{
		resize(size);
		move(pos);
	}
	ui->m_pTabWidget->setTabText(0, "New File");
}